home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java for 3D & VRML Worlds
/
Java for 3d and VRML Worlds.iso
/
world
/
wave
/
wave.java
< prev
next >
Wrap
Text File
|
1996-10-18
|
5KB
|
159 lines
// "Wave Effect"
// created by ask@krc.sony.co.jp (Masamichi zzzcat Asukai)
//
// Copyright(C) 1996 Sony Corporation. All rights reserved.
//
import vrml.*;
import vrml.node.*;
import vrml.field.*;
public class wave extends Script {
private SFFloat getFraction;
private float fraction;
private Node coord;
private int type;
private float amplitude;
private float frequency;
private float[] origin = new float[2];
private float[] direction = new float[2];
private boolean set_flag;
private MFVec3f point;
private int n, i;
private float p[] = null, y[] = null;
private float d;
private float PI = 3.14f;
// constructor
public void initialize() {
try {
getFraction = (SFFloat)getEventIn("fraction");
// get field values of script node
coord = (Node)((SFNode)getField("coord")).getValue();
type = (int)((SFInt32)getField("type")).getValue();
amplitude = (float)((SFFloat)getField("amplitude")).getValue();
frequency = (float)((SFFloat)getField("frequency")).getValue();
set_flag = (boolean)((SFBool)getField("set_flag")).getValue();
switch (type) {
default:
case 0:
((SFVec2f)getField("origin")).getValue(origin);
break;
case 1:
((SFVec2f)getField("direction")).getValue(direction);
if (0.0f == direction[0] && 0.0f == direction[1]) {
direction[0] = 1.0f; direction[1] = 0.0f;
} else {
// normalize direction
d = (float)java.lang.Math.
sqrt(direction[0] * direction[0] +
direction[1] * direction[1]);
direction[0] /= d; direction[1] /= d;
}
break;
}
// get vertices of coord
point = (MFVec3f)coord.getExposedField("point");
n = point.getSize();
p = new float[n*3];
point.getValue(p);
// save initial Y value of vertices
if (true == set_flag) {
y = new float[n];
for (i=0; i<n; ++i) {
y[i] = p[i*3+1];
}
}
} catch(Exception e) {
System.out.println("initialize error");
e.printStackTrace();
}
}
public void processEvent(Event e) {
if (e.getName().equals("fraction")) {
// get current position
point.getValue(p);
// set initial Y value if set_flag is TRUE
if (true == set_flag) {
for (i=0; i<n; ++i) {
p[i*3+1] = y[i];
}
}
fraction = (2.0f * PI) * getFraction.getValue();
switch (type) {
default:
case 0:
for (i=0; i<n; ++i) {
d = (float)java.lang.Math.
sqrt((p[i*3] - origin[0]) * (p[i*3] - origin[0]) +
(p[i*3+2] - origin[1]) * (p[i*3+2] - origin[1]));
p[i*3+1] += amplitude *
easyCos(frequency * d - fraction);
// (float)java.lang.Math.cos(frequency * d - fraction);
}
break;
case 1:
for (i=0; i<n; ++i) {
d = p[i*3] * direction[0] + p[i*3+2] * direction[1];
p[i*3+1] += amplitude *
easyCos(frequency * d - fraction);
// (float)java.lang.Math.cos(frequency * d - fraction);
}
break;
}
// set calculated positions to vertices of coord
point.setValue(n*3,p);
}
}
// simple cosine function by using linear interpolation
float easyCos(float x) {
float yi, yj;
// map x between 0.0f and PI
if (x < 0.0f) {
x = -x;
}
x -= (int)(x / (2.0f * PI)) * (2.0f * PI);
if (PI <= x) {
x = (2.0f * PI) - x;
}
// linear interpolation
if (x < 0.2f*PI) {
x = x / (0.2f * PI);
yi = 1.0f; // cos(0)
yj = 0.809f; // cos(0.2*PI)
} else if (x < 0.4f*PI) {
x = (x - 0.2f * PI) / (0.2f * PI);
yi = 0.809f; // cos(0.2*PI)
yj = 0.309f; // cos(0.4*PI)
} else if (x < 0.6f*PI) {
x = (x - 0.4f * PI) / (0.2f * PI);
yi = 0.309f; // cos(0.4*PI)
yj = -0.309f; // cos(0.6*PI)
} else if (x < 0.8f*PI) {
x = (x - 0.6f * PI) / (0.2f * PI);
yi = -0.309f; // cos(0.6*PI)
yj = -0.809f; // cos(0.8*PI)
} else {
x = (x - 0.8f * PI) / (0.2f * PI);
yi = -0.809f; // cos(0.8*PI)
yj = -1.0f; // cos(PI)
}
return ((1.0f - x) * yi + x * yj);
}
}